package com.woniu.market_stall.web;


import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.PermissionService;
import com.woniu.market_stall.vo.PermissionVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService ;

    @GetMapping("/showMenuByManager/{id}")
    @ApiOperation(value = "本接口是限制管理员所拥有角色能查看的菜单")
    public ResultResponse<List<PermissionVo>> showMenuByManager(@PathVariable("id") int mid){
        ResultResponse<List<PermissionVo>> result = null;

        try{
            if (mid == 1){
                List<PermissionVo> list = permissionService.queryAllMenu();
                result = new ResultResponse<>(200,"查询成功",list);
            }else {
                List<PermissionVo> list = permissionService.queryMenuByManager(mid);
                result = new ResultResponse<>(200,"查询成功",list);
            }

        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败");
        }
        return result ;
    }


    @GetMapping("/showAllMenu")
    @ApiOperation(value = "本接口查询所有菜单")
    public ResultResponse<List<PermissionVo>> showAllMenu(){
        ResultResponse<List<PermissionVo>> result = null;

        try{
            List<PermissionVo> list = permissionService.queryAllMenu();
            result = new ResultResponse<>(200,"查询成功",list);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败");
        }
        return result ;
    }


    @GetMapping("/getPermissionId/{id}")
    @ApiOperation(value = "本接口用于获取授权")
    public ResultResponse<List<Long>> getPermissionId(@PathVariable("id") int id){
        ResultResponse<List<Long>> result = null;

        try {
            List<Long> list = permissionService.queryPermissionIdByRoleId(id);
            result = new ResultResponse<>(200,"查询成功!",list);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }








}
